package com.example.community.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.community.dao.mapper.LoginTicketMapper;
import com.example.community.dao.pojo.LoginTicket;
import com.example.community.dao.pojo.User;
import com.example.community.service.LoginTicketService;
import com.example.community.service.UserService;
import com.example.community.uitls.CommunityUtil;
import com.example.community.uitls.RedisKeyUtil;
import com.mysql.cj.log.Log;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: LoginTicketServiceImpl
 * @Description:
 * @Author
 * @Date 2022-03-17
 * @Version 1.0
 */
@Service
public class LoginTicketServiceImpl implements LoginTicketService {

//    @Autowired
//    LoginTicketMapper loginTicketMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

//    @Override
//    public int insertLoginTicket(LoginTicket loginTicket) {
//        String redisKey = loginTicket.getTicket();
//        redisTemplate.opsForValue().set(redisKey,loginTicket);
//        return redisTemplate.opsForValue().set(redisKey,loginTicket);
//    }

    @Override
    public LoginTicket findLoginTicket(String ticket) {
        String redisKey = RedisKeyUtil.getTicketKey(ticket);
        return (LoginTicket) redisTemplate.opsForValue().get(redisKey);
    }

    @Override
    public void logout(String ticket) {
        String redisKey = RedisKeyUtil.getTicketKey(ticket);
        LoginTicket loginTicket = (LoginTicket) redisTemplate.opsForValue().get(redisKey);
        loginTicket.setStatus(1);
        redisTemplate.opsForValue().set(redisKey,loginTicket);
    }

    @Override
    public Map<String, Object> login(String username, String password, int expiredSeconds) {
        Map<String, Object> map = new HashMap<>();
        //空值处理
        if (StringUtils.isBlank(username)){
            map.put("usernameMsg","账号不能为空！");
            return map;
        }
        if (StringUtils.isBlank(password)){
            map.put("passwordMsg","密码不能为空！");
            return map;
        }

        //验证账号和状态
        User u = userService.findUserByUsername(username);
        if (u==null){
            map.put("usernameMsg","此账号不存在！");
            return map;
        }
        if (u.getStatus()==0){
            map.put("usernameMsg","此账号未激活！");
            return map;
        }
        //验证密码
        password = CommunityUtil.md5(password + u.getSalt());
        if (!u.getPassword().equals(password)){
            map.put("passwordMsg","密码不正确，请重新输入！");
            return map;
        }


    // 生成登录凭证
        LoginTicket loginTicket = new LoginTicket();
        loginTicket.setUserId(u.getId());
        loginTicket.setTicket(CommunityUtil.generateUUID());
        loginTicket.setStatus(0);
        loginTicket.setExpired(new Date(System.currentTimeMillis() + expiredSeconds * 1000));
//        loginTicketMapper.insert(loginTicket);

        String redisKey = RedisKeyUtil.getTicketKey(loginTicket.getTicket());
        redisTemplate.opsForValue().set(redisKey,loginTicket);

        map.put("ticket", loginTicket.getTicket());
        return map;
    }


}
